//
//  LSWLoginViewController.swift
//  iPad_QQ
//
//  Created by 李善武 on 16/4/29.
//  Copyright © 2016年 李善武. All rights reserved.
//

import UIKit

class LSWLoginViewController: LSWBaseViewController {

    @IBOutlet weak var accountTF: UITextField!
    
    @IBOutlet weak var passwordTF: UITextField!
    
    @IBOutlet weak var loginBtn: UIButton!
    
    @IBOutlet weak var activityView: UIActivityIndicatorView!
    
    @IBOutlet weak var rememberBtn: UIButton!
    
    @IBOutlet weak var autoLogin: UIButton!
    
    @IBOutlet weak var loginView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

}


// MARK:- UITextFieldDelegate
extension LSWLoginViewController : UITextFieldDelegate {
    /// 当点击return按钮的时候会执行该方法
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField == accountTF { // 帐号->点击return
            // 让密码输入框称为第一响应者
            passwordTF.becomeFirstResponder()
        } else if textField == passwordTF {
            // 登录
            loginBtnClick(loginBtn)
        }
        
        return true
    }
}

extension LSWLoginViewController {
    @IBAction func loginBtnClick(sender: UIButton) {
         //1.退出键盘
        view.endEditing(true)
        
        // 2.得到账号和密码
        let account = accountTF.text
        let password = passwordTF.text
        
        // 3.判断账号和密码输入框是否有值
        if account?.isEmpty == true || password?.isEmpty == true{
            
            showError("用户名和密码不能为空")
            return
        }
        // 4.开始转圈
        activityView.startAnimating()
        view.userInteractionEnabled = false
        
        let time : dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC))
        dispatch_after(time, dispatch_get_main_queue()) { () -> Void in
            self.activityView.stopAnimating()
            self.view.userInteractionEnabled = true
            // 4.判断账号和密码是否输入正确
            if account == "123" && password == "123"{
                // 弹出主页控制器
                self.view.window?.rootViewController = HomeViewController()
               
            }else{
                print("请输入正确的密码和用户名")
            }
            
        }
        
    }
    
    private func showError(errorMessage : String){
        // 1.弹框提示
        let alertController = UIAlertController(title: "登录失败", message: errorMessage, preferredStyle: .Alert)
        let alertAction = UIAlertAction(title: "确定", style: .Cancel, handler: nil)
        alertController.addAction(alertAction)
        presentViewController(alertController, animated: true, completion: nil)
        // 2.抖动效果
        let shakeAnim = CAKeyframeAnimation(keyPath: "transform.translation.x")
        shakeAnim.duration = 0.1
        shakeAnim.repeatCount = 3
        shakeAnim.values = [-10,0,10]
        loginView.layer.addAnimation(shakeAnim, forKey: nil)
    }


}

// 记住密码和自动登录逻辑
extension LSWLoginViewController {

    
    
    @IBAction func remberBtnClikc(sender: UIButton) {
        // checkBox状态反选
        rememberBtn.selected = !sender.selected
        // 若记住密码不被选中，则自动登录也不被选中
        if sender.selected == false{
            autoLogin.selected = false
        }
    }
    
    
    @IBAction func autoLoginBtnClick(sender: UIButton) {
        autoLogin.selected = !sender.selected
        
        if sender.selected == true{
            rememberBtn.selected = true
        }
    }
    
}



